Skip to content

turbo: set the Thumb bit on code pointers only when compiling for Thumb - #11361

Open
mikeysklar wants to merge 1 commit into
adafruit:mainfrom
mikeysklar:ptr-callable-v128
Open

turbo: set the Thumb bit on code pointers only when compiling for Thumb#11361
mikeysklar wants to merge 1 commit into
adafruit:mainfrom
mikeysklar:ptr-callable-v128

Conversation

@mikeysklar

@mikeysklar mikeysklar commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

What

Turbo needs this to support XTENSA and RISC-V.

Bit 0 is set only when __thumb__ is defined. Other targets get the pointer unchanged.

Why

Every native call uses this macro. On Xtensa and RISC-V, bit 0 lands one byte past the code and hard-faults.

MICROPY_MAKE_POINTER_CALLABLE
MicroPython py/mpconfig.h (p); each port overrides
MicroPython stm32, rp2, samd, nrf, mimxrt, renesas-ra | 1
MicroPython esp32, esp8266 plain cast
CircuitPython py/circuitpy_mpconfig.h:202, all ports | 1
Guard is __thumb__, not MICROPY_EMIT_THUMB RP2 loads native .mpy with the emitter off
Effect on main none: only used under MICROPY_ENABLE_NATIVE_CODE or MICROPY_EMIT_INLINE_ASM, which no non-Thumb board enables

Hardware tested

Build only. Main at 42af95d52e, GCC 15.2.1, Ubuntu 24.04.

Board main this PR bytes differing
Metro M4 AirLift Lite 488,912 B 488,912 B 35, version strings and one merged-string pointer ("e")
Metro RP2350 924,332 B 924,332 B 44, version strings only

How I tested it

Built each board at main and at this PR, then compared firmware.bin byte by byte.

build-pr3cmp-main-metro_m4_airlift_lite 488912 B vs build-pr3cmp-pr3-metro_m4_airlift_lite 488912 B, bytes differing: 35
< 3.4.0; CircuitPython 10.4.0-alpha.1-3-g42af95d52e on 2026-09-12
> 3.4.0; CircuitPython 10.4.0-alpha.1-4-g0b83ba43dc on 2026-09-12
build-pr3cmp-main-adafruit_metro_rp2350 924332 B vs build-pr3cmp-pr3-adafruit_metro_rp2350 924332 B, bytes differing: 44
< 10.4.0-alpha.1-3-g42af95d52e
> 10.4.0-alpha.1-4-g0b83ba43dc

Scope

Needed before native .mpy can run on Espressif. No functional change on any board in main.

AI assistance

Claude Code was used.

On Xtensa and RISC-V, bit 0 makes native calls jump to an odd address.
ARM needs it for Thumb.

Set it only when __thumb__ is defined. RP2 loads native .mpy with the
emitter off, so not MICROPY_EMIT_THUMB.

No change on main: no non-ARM port runs native code yet.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@mikeysklar mikeysklar changed the title py: set the Thumb bit on code pointers only when compiling for Thumb turbo: set the Thumb bit on code pointers only when compiling for Thumb Sep 13, 2026

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - makes sense. A little surprised we didn't hit this before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants